home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / amitcp_session.lha / AmiTCP_session-1.0 / c / AmiTCP_session.rexx next >
Encoding:
OS/2 REXX Batch file  |  1994-08-06  |  6.6 KB  |  285 lines

  1. /*
  2.  * AmiTCP_session version 1.0
  3.  *
  4.  * Arexx script to dial a SLIP server, start AmiTCP, send mail, etc.
  5.  * Usage: AmiTCP_session.rexx
  6.  *
  7.  * Written by David Jameson, 1994
  8.  * based on AmiTCP_dialup by K. Raquel Sanborn
  9.  */
  10.  
  11. /* Constants */
  12.  
  13. cr              = '0d'x
  14. hangup_str      = 'ATH'||cr
  15. phone_number    = 'PHONE'        /* Put telephone number here */
  16. username        = 'USERNAME'||cr         /* Put your user name here */
  17. passwd          = 'PASSWORD'||cr         /* Put your password here */
  18. logfile     = 'ncomm:ncomm.log'    /* Put name of log-file here */
  19. nntpserver    = 'news.gpl.net'    /* Put name of NNTP server here */
  20. serial_speed    = 57600            /* Put speed of serial port here */
  21. slip_device    = 'rhcslip.device'    /* Name of slip device */
  22. active_file    = 'uulib:active'    /* Name of active file */
  23. dialstr         = 'ATDT'||phone_number||cr
  24.  
  25.  
  26. /* Open libraries */
  27.  
  28. if ~show('L', 'rexxsupport.library') then do
  29.     if addlib('rexxsupport.library', 0, -30, 0) then
  30.     say 'added rexxsupport.library'
  31.     else do
  32.     say 'failed to open rexxsupport.library'
  33.     exit 10
  34.     end
  35. end
  36.  
  37. if ~show('L', 'rexxserdev.library') then do
  38.     if addlib('rexxserdev.library', 0, -30, 5) then
  39.     say 'added rexxserdev.library'
  40.     else do
  41.     say 'failed to open rexxserdev.library'
  42.     exit 10
  43.     end
  44. end
  45.  
  46. say 'Starting AmiTCP...'
  47. address command
  48. 'amitcp:bin/startnet'
  49.  
  50. say 'Starting smtpd...'
  51. 'run smtpd'
  52.  
  53. serhandle = SerOpen('serial.device', 0)
  54. if serhandle = 0 then do
  55.     say 'Failed to open serial device'
  56.     exit 1
  57. end
  58.  
  59. check     = SerClear(serhandle)
  60. check     = SerSetParms(serhandle, serial_speed, 8, N, 1, 0 )
  61.  
  62. buffer = allocmem(4096)
  63. junk   = c2d(buffer)
  64.  
  65. /* Clean up when ctrl-C pressed */
  66. signal on break_c
  67.  
  68. say 'Dialing the terminal server... '
  69. check    = 0
  70. do forever
  71.     if (check == 0) then
  72.     do
  73.     result = SerWrite(serhandle, dialstr, length(dialstr))
  74.     check = 1
  75.     totalstr = ''
  76.     end
  77.     status   = SerQuery(serhandle)
  78.     parse upper var status valid bytes_read status_bits .
  79.     rcvdstr  = SerRead(serhandle, junk, bytes_read)
  80.     totalstr = totalstr||rcvdstr
  81.     check    = modem_query(totalstr)
  82.     if (check == 100) then leave
  83.     result   = delay(100)
  84. end
  85.  
  86. totalstr = ''
  87.  
  88. check = SerFlush(serhandle, 'R')
  89. say 'Modem has connected to the terminal server...'
  90.  
  91. check = 0
  92. do until check = 1
  93.     say 'Sending CR to server...'
  94.     check = SerWrite(serhandle, cr, length(cr))
  95.     check = wait_str(':')
  96. end
  97.  
  98. say 'Received user prompt...'
  99. check = SerWrite(serhandle, username, length(username))
  100. check = SerRead(serhandle, junk, length(username))
  101. say 'Sent Username, now waiting for password prompt...'
  102.  
  103. check = 0
  104. do until check = 1
  105.     check = wait_str(':')
  106. end
  107. say 'Received password prompt...'
  108. check = SerWrite(serhandle, passwd, length(passwd))
  109. say 'Sent password...'
  110.  
  111. check = SerClose(serhandle)
  112. serhandle = 0        /* Just in case ctrl-C is pressed */
  113. check = freemem(buffer,4096)
  114. buffer = 0
  115.  
  116. say 'Putting SLIP online'
  117. 'online' slip_device '0'
  118.  
  119. /* First try to open existing log file */
  120. check = open('log', logfile, 'a')
  121.  
  122. /* That didn't work, so now create a new log file */
  123. if (check == 0) then do
  124.     check = open('log', logfile, 'w')
  125.     if (check == 0) then do
  126.     say "Failed to open log file"
  127.     exit
  128.     end
  129. end
  130.  
  131. say 'Logging start of call...'
  132. check = writeln('log', 'AMITCP');
  133. check = writeln('log', '--------------------------------');
  134. weekday = left(date(weekday), 3)
  135. date = date()
  136. parse var date day month year
  137. time1 = time()
  138. parse var time1 hour1 ':' min1 ':' sec1
  139. check = writeln('log', 'Login: ' weekday month day||' '||time1||year);
  140.  
  141. say 'Sending outgoing mail (if any)...'
  142. 'breakname smtpd f'
  143.  
  144. say 'Sending outgoing news (if any)...'
  145. 'list uunews:NNTPSpoolDir lformat "nntppost <%p%n*ndelete %p%n" to t:post'
  146. 'execute t:post'
  147. 'delete >nil: t:post'
  148.  
  149. say 'Beginning nntp transfer...'
  150. 'run nntpxfer' nntpserver active_file
  151.  
  152. /* Wait until user clicks on the 'OK' button */
  153. 'RequestChoice "AmiTCP Front-end" "Click to close AmiTCP session" QUIT >nil:'
  154.  
  155. say 'Killing AmiTCP...'
  156. 'offline' slip_device '0'    /* Just in case AmiTCP doesn't get killed */
  157.  
  158. address AMITCP
  159. 'KILL'
  160. check = delay(150)
  161.  
  162. say 'Closing connection...'
  163.  
  164. serhandle = SerOpen('serial.device', 0)
  165. check     = SerClear(serhandle)
  166. check     = SerSetParms(serhandle, serial_speed, 8, N, 1, 0)
  167.  
  168. /* Send +++, wait the send ATH, then exit */
  169.  
  170. say 'Sending +++'
  171. check = delay(75)
  172. check = SerWrite(serhandle, '+++', length('+++'))
  173. check = delay(75)
  174. say 'Sending ATH<cr>'
  175. check = SerWrite(serhandle, 'A', 1)
  176. check = delay(2)
  177. check = SerWrite(serhandle, 'T', 1)
  178. check = delay(2)
  179. check = SerWrite(serhandle, 'H', 1)
  180. check = delay(2)
  181. check = SerWrite(serhandle, cr, 1)
  182. check = delay(25)
  183. check = SerClose(serhandle)
  184. serhandle = 0
  185.  
  186. say 'Logging end of call...'
  187.  
  188. time2 = time()
  189. date = date()
  190. weekday = left(date(week), 3)
  191. parse var date day month year
  192. check = writeln('log', 'Logout:' weekday month day||' '||time2||year);
  193. parse var time2 hour2 ':' min2 ':' sec2
  194.  
  195. /* Calculate time online */
  196. diffsecs = sec2 - sec1
  197. diffmins = min2 - min1
  198. diffhours = hour2 - hour1
  199. if diffsecs < 0 then do
  200.     diffsecs = diffsecs + 60
  201.     diffmins = diffmins - 1
  202. end
  203. if diffmins < 0 then do
  204.     diffmins = diffmins + 60
  205.     diffhours = diffhours - 1
  206. end
  207. if diffhours < 0 then do
  208.     diffhours = diffhours + 24
  209. end
  210.  
  211. /* Pad out digits with leading 0s */
  212. diffsecs = right(diffsecs, 2, '0')
  213. diffmins = right(diffmins, 2, '0')
  214. diffhours = right(diffhours, 2, '0')
  215.  
  216. check = writeln('log', 'Time online: 'diffhours||':'||diffmins||':'||diffsecs);
  217. check = writeln('log', '')
  218. check = close('log')
  219.  
  220. say 'Done!'
  221.  
  222. exit 0
  223.  
  224.  
  225. /*
  226.  * Procedure to determine the current status of the modem
  227.  */
  228. modem_query: procedure
  229.  
  230.     teststr = arg(1)
  231.     connectstr       = 'CONNECT'
  232.     busystr          = 'BUSY'
  233.     nocarrierstr     = 'NO CARRIER'
  234.  
  235.     test = pos(connectstr, teststr)
  236.     if (test ~= 0) then return 100
  237.  
  238.     test = pos(busystr, teststr)
  239.     if (test ~= 0) then return 0
  240.  
  241.     test = pos(nocarrierstr, teststr)
  242.     if (test ~= 0) then return 0
  243.  
  244.     return 200
  245.  
  246.  
  247. /*
  248.  * Subroutine to wait until a string is received from the serial port.
  249.  */
  250. wait_str:
  251.  
  252.     teststr = arg(1)
  253.     check = 0
  254.     count = 0
  255.     do until ((check ~= 0) | (count > 5))
  256.         status = SerQuery(serhandle)
  257.         parse upper var status valid bytes_read status_bits .
  258.         rcvdstr = SerRead(serhandle, junk, bytes_read)
  259.         check   = pos(teststr,rcvdstr)
  260.         test    = delay(50)
  261.     count = count + 1
  262.     end
  263.     if (count > 5) then return 0
  264. return 1
  265.  
  266. /*
  267.  * Ctrl-C signal handling routine.
  268.  */
  269. break_c:
  270.     say 'Cleaning up...'
  271.     if (serhandle ~= 0) then
  272.     check = SerClose(serhandle)
  273.  
  274.     if (buffer ~= 0) then
  275.     check = freemem(buffer, 4096)
  276.  
  277.     address command
  278.     'offline' slip_device '0'    /* Just in case AmiTCP doesn't get killed */
  279.  
  280.     address AMITCP
  281.     'KILL'
  282.     check = delay(150)
  283. exit 20
  284.  
  285.